/*
 * Copyright (c) 2009, Jan Pastor. All Rights Reserved.
 */

package sk.jp.jitag.dao;

import javax.transaction.TransactionRolledbackException;

/**
 * Base DAO interface.
 */
public interface Dao {

    /**
     * Start database transaction.
     */
    void beginTransaction();

    /**
     * Commit database transaction.
     */
    void commitTransaction();

    /**
     * Rollback all changes performed during current database transaction.
     * @param cause exception that causes rollback. (null permitted)
     */
    void rollbackTransaction(Throwable cause);

    /**
     * Runs given task in a transaction.
     * <p>If no transaction was started before, strats new. On the end of task,
     * commit or rollback is performed<s> and finally is this dao closed</s>.
     * @param task task to run in one transaction.
     * @throws TransactionRolledbackException if rollback occurrs. See
     * {@link TransactionRolledbackException} description for what you can do
     * in this case - refresh objects.
     */
    void doInTransaction(Runnable task);

    /**
     * Performs close operation for DAO.
     */
    public void close();

}
